package com.xiashitech.agent.utils;

import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;

public class DurationPrinter {
    private long startpoint = 0L;
    private long endpoint = 0L;
    private String descInfoStr = "";
    private int pos = 0;

    public DurationPrinter(String info) {
        startpoint = System.nanoTime();
        descInfoStr = info;
    }

    public void printElapseDuration() {
        printElapseDuration("");
    }

    public void printElapseDuration(String posInfo) {
        if(!InstrumentationConfig.get().getBoolean("xs.profile.log",false)) {
            return;
        }
        String extInfo = posInfo;
        if(extInfo == null || "".equals(extInfo)) {
            extInfo = "A A";
        }
        pos++;
        endpoint = System.nanoTime();
        long duration = endpoint - startpoint;
        String str = String.format("%d T%d %s %s POS%02d elapsetime %d ms %d ns", Thread.currentThread().getId(), endpoint, descInfoStr, extInfo, pos, (duration/1000000), duration);
        System.out.println(str);
        startpoint = System.nanoTime();
    }

    public void setCurrentStart() {
        startpoint = System.nanoTime();
        endpoint = System.nanoTime();
    }
}
